package com.lagou.work5.server;

import java.io.*;
import java.net.Socket;

public class ServerReceiver implements Runnable{
    private String userName;
    private int socketNum;

    public ServerReceiver(String userName, int socketNum) {
        this.userName = userName;
        this.socketNum = socketNum;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getSocketNum() {
        return socketNum;
    }

    public void setSocketNum(int socketNum) {
        this.socketNum = socketNum;
    }

    @Override
    public void run() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(Server.users[socketNum].getInputStream()));

            // 不断获取该用户的消息
            while (true){
                String userMessage = bufferedReader.readLine();
                System.out.println(userName+"->"+userMessage);
                if("sendFile".equals(userMessage)){
                    //1、接收文件名
                   String fileName =  bufferedReader.readLine();
                    // 2、读取文件存储文件至服务器
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(Server.users[socketNum].getInputStream());
                     // 读取文件存储下来
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("/Users/dingxiaowei/Downloads/server/"+fileName));
                    int res  = 0;
                    while ((res = bufferedInputStream.read())!=-1){
                        bufferedOutputStream.write(res);
                        // 存储到本地
                        bufferedOutputStream.flush();
                    }
                    System.out.println(fileName+"发送的文件已经保存在了服务器");
                    // 3、消息分发
                    userMessage="发送了"+fileName+"文件";
                    messageDispatch(userName,socketNum,userMessage);

                }else if("downloadFile".equals(userMessage)){
                    // 1、接收文件名
                    String fileName = bufferedReader.readLine();
                    // 2、获取文件并发送
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("/Users/dingxiaowei/Downloads/server/"+fileName));
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(Server.users[socketNum].getOutputStream());
                    int res = 0;
                    while ((res=bufferedInputStream.read())!=-1){
                        bufferedOutputStream.write(res);
                        bufferedOutputStream.flush();
                    }
                    System.out.println(userName+"下载了文件");
                    userMessage = "下载了"+fileName+"文件";
                    messageDispatch(userName,socketNum,userMessage);
                }else {
                    System.out.println(userName+"发送了消息："+userMessage);
                    messageDispatch(userName,socketNum,userMessage);
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        }





    }

    public void messageDispatch(String userName,int socketNum,String userMessage){
        for(int i=0;i<Server.socketNum;i++){
            if(i==socketNum){
                continue;
            }
            // 获取对应用户的输出流，发送消息
            try {
                PrintStream printStream = new PrintStream(Server.users[i].getOutputStream());
                printStream.println(userName+":"+userMessage);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
